package zero.tech.games.gate.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Component
@Configuration
@EnableAutoConfiguration
public class MvcInterceptor implements WebMvcConfigurer {
	@Autowired
	private CheckUserLastRoomInterceptor checkUserLastRoomInterceptor;
	@Autowired
	private CheckPipeiQueueInterceptor checkPipeiQueueInterceptor;
	@Autowired
	private CharsetInterceptor charsetInterceptor;

//	@Override
//	public void addCorsMappings(CorsRegistry registry) {
//		registry.addMapping("/**").allowedHeaders("*").allowedMethods("*").allowedOrigins("*").allowCredentials(true).maxAge(3600);
//	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(charsetInterceptor).addPathPatterns("/**");
		registry.addInterceptor(checkUserLastRoomInterceptor).addPathPatterns("/**");
		registry.addInterceptor(checkPipeiQueueInterceptor).addPathPatterns("/**");
	}
	
//    @Bean
//    public CorsFilter corsFilter() {
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        source.registerCorsConfiguration("/**", buildConfig());
//        return new CorsFilter(source);
//    }
    
//	private CorsConfiguration buildConfig() {
//        CorsConfiguration corsConfiguration = new CorsConfiguration();
//        corsConfiguration.addAllowedOrigin("*");
//        corsConfiguration.addAllowedHeader("*");
//        corsConfiguration.addAllowedMethod("*");
//        corsConfiguration.setAllowCredentials(true);
//        corsConfiguration.setMaxAge(3600L);
//        return corsConfiguration;
//    }

}
